﻿/*! @file
********************************************************************************
<PRE>
文 件 名    : SelectorFrm.cs
说    明    : 转换目标的版本选择界面
作    者    : 汪泽然
--------------------------------------------------------------------------------
备    注    : 
--------------------------------------------------------------------------------
修改记录: 
 * 日   期      版  本       修改人      修改内容 
 * 2009-7-11    0.5.0       汪泽然       选择转换的目标版本的界面
 * 2009.8.4     0.5.1       汪泽然       选择版本时加上目标版本的框架版本
</PRE>
********************************************************************************
* 版权所有(c) 2008 ~ 2009, 汪泽然, 保留所有权利
*******************************************************************************/
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Wangzeran.Transform.Code;
using Wangzeran.Transform.Core.Log;
using Wangzeran.Transform.Resources;
using Wangzeran.Transform.Core.TransformException;
using System.Text;
using Wangzeran.Transform.Core.Support;

namespace Wangzeran.Transform.Forms
{
    /// <summary>
    /// 转换目标的版本选择界面
    /// </summary>
    public partial class SelectorFrm : Form
    {
        /// <summary>
        /// 正常的高度
        /// </summary>
        private const int HEIGHT_NORMAL = 175;
        /// <summary>
        /// 最大化的高度
        /// </summary>
        private const int HEIGHT_FULL = 335;
        /// <summary>
        /// 要变化的次数
        /// </summary>
        private const int TIMES = 16;
        /// <summary>
        /// 线程的睡眠时间（值越小，速度越快）
        /// </summary>
        private const int SELLP_TIME = 20;

        /// <summary>
        /// 解决方案文件完整路径
        /// </summary>
        private string filePath = String.Empty;


        /// <summary>
        /// 初始化版本信息
        /// </summary>
        private void Init()
        {
            try
            {
                string version = TransMain.GetSlnVersion(filePath);
                lblCurrentVersion.Text = version;

                cboTargetVersion.Items.Clear();

                if (PlugIns.RegisterPlugIns.Count == 0)
                {
                    ShowTips("暂无插件支持转换", Color.Red);
                    btnTransform.Enabled = false;
                    return;
                }

                foreach (BaseTransformFactory factory in PlugIns.RegisterPlugIns.Values)
                {
                    string item = factory.TargetIDEVersion + "【" + factory.TargetLanguage + "】" 
                        + factory.TargetFrameworkVersion;
                    cboTargetVersion.Items.Add(item);
                }
            }
            catch (BaseException)
            {
                ShowTips("初始化失败", Color.Red);
                txtMessage.Text = LogFactory.LogContents.ToString();
                btnShowMessage.Enabled = true;
                btnTransform.Enabled = false;
            }
            catch (Exception ex)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(LogFactory.LogContents.ToString());
                sb.AppendLine(ex.Message);
                ShowTips("初始化失败", Color.Red);
                txtMessage.Text = sb.ToString();
                btnShowMessage.Enabled = true;
                btnTransform.Enabled = false;
            }

        }

        /// <summary>
        /// 实例化版本选择界面
        /// </summary>
        private SelectorFrm()
        {
            InitializeComponent();

            timerOpacity.Start();
        }

        /// <summary>
        /// 实例化版本选择界面
        /// </summary>
        /// <param name="filePath">解决方案文件完整路径</param>
        public SelectorFrm(string filePath) : this()
        {
            this.filePath = filePath;
            Init();
        }

        /// <summary>
        /// 加载界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectorFrm_Load(object sender, EventArgs e)
        {
            this.Text = AppResource.AppName + "  " +
                AppResource.AppVersion;

            this.Height = HEIGHT_NORMAL;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.ExitThread();
        }

        /// <summary>
        /// 显示详细信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnShowMessage_Click(object sender, EventArgs e)
        {
            int preHeight = (HEIGHT_FULL - HEIGHT_NORMAL) / TIMES;
            if (this.Height >= HEIGHT_FULL)
            {
                for (int i = 0; i < TIMES; i++)
                {
                    this.Height -= preHeight;
                    Thread.Sleep(SELLP_TIME);
                }
                gbMessage.Visible = false;
            }
            else
            {
                gbMessage.Visible = false;
                for (int i = 0; i < TIMES; i++)
                {
                    this.Height += preHeight;
                    Thread.Sleep(SELLP_TIME);
                }
                gbMessage.Visible = true;
            }
        }

        /// <summary>
        /// 转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTransform_Click(object sender, EventArgs e)
        {
            try
            {
            	txtMessage.Text = String.Empty;
                btnShowMessage.Enabled = false;
                if(this.Height >= HEIGHT_FULL)
                {
                	btnShowMessage_Click(null, null);
                }
            	
                if (cboTargetVersion.SelectedIndex == -1)
                {
                    ShowTips("请选择目标版本", Color.Red);
                    return;
                }

                ShowTips("正在转换...", Color.Blue);

                string item = cboTargetVersion.SelectedItem.ToString();

                bool result = TransMain.Transform(item.Remove(item.LastIndexOf("【")), 
                    filePath);

                ShowTips("转换成功", Color.Green);

                if (ckAutoExit.Checked)
                {
                    btnExit_Click(null, null);
                }

                LogFactory.ClearLog();

                Init();
            }
            catch
            {
            	ShowTips("转换失败", Color.Red);
                txtMessage.Text = LogFactory.LogContents.ToString();
                btnShowMessage.Enabled = true;
                if (ckAutoError.Checked)
                {
                    btnShowMessage_Click(null, null);
                }
            }
        }

        /// <summary>
        /// 显示提示信息
        /// </summary>
        /// <param name="message">提示信息文本</param>
        /// <param name="color">提示信息颜色</param>
        private void ShowTips(string message, Color color)
        {
            lblResult.ForeColor = color;
            lblResult.Text = message;
            if (!lblResult.Visible)
            {
                lblResult.Visible = true;
            }
        }


        /// <summary>
        /// 透明检测的定时器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timerOpacity_Tick(object sender, EventArgs e)
        {
            CheckMouseUpForm();
        }

        /// <summary>
        /// 检查鼠标是否在窗体上，是的话就设置为不透明，否则设为半透明
        /// </summary>
        private void CheckMouseUpForm()
        {
            Point frmPoint = this.Location;
            Point mousePoint = MousePosition;

            if (mousePoint.X > frmPoint.X
                && mousePoint.X < (frmPoint.X + this.Width)
                && mousePoint.Y > frmPoint.Y
                && mousePoint.Y < (frmPoint.Y + this.Height))
            {
                if (this.Opacity != 1)
                {
                    this.Opacity = 1;
                }
            }
            else
            {
                if (this.Opacity == 1)
                {
                    this.Opacity = 0.6;
                }
            }
        }

    }
}
